angular.module('user',[]).config(['$httpProvider',function($httpProvider){
	 $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
     $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
}]).factory('user',function($http){
	var baseUrl='/index.php/User';
	return {
		get:function(userId){
            return $http.get(baseUrl+'/info/'+userId);
		},
		save:function(user){
            var url=user.id?baseUrl+'/modify/'+user.id:baseUrl;
            $http.post(url,user).success(function(data,status,headers,config){
	            alert(data.msg);
            }).error(function(data,status,headers,config){
            	alert('未响应！');
            });
		},
		query:function(){
			return $http.get(baseUrl+'/list');
		},
		create:function(user){
			var postData='name='+user.name+'&email='+user.email+'&password='+user.password;
           $http.post(baseUrl+'/add',postData).success(function(data,status,headers,config){
	            alert(data.msg);
           }).error(function(data,status,headers,config){
           	   alert('未响应！');
           });
		}
	}
}).controller('RegisterCtrl',function(user,$scope,$http){
	$scope.user={"name":'',"email":'',"sex":'',"age":'',"password":'',"repassword":''};
	$scope.error_msg={msg:'',show:false};
	$scope.register=function(){
		if($scope.user.password!=$scope.user.repassword){
           $scope.error_msg.show=true;
          $scope.error_msg.msg='两次密码输入不一致！';
           return ;
		}
		user.create($scope.user);
	}
	$scope.reset=function(){
		$scope.user={name:'',email:'',sex:'',age:'',password:''};
	}

});